package base.前缀树;

import javafx.beans.property.ObjectProperty;
import jdk.nashorn.internal.ir.CallNode;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2021/3/5 0005 13:35
 * 默写前缀树代码
 * 3
 * 2
 * 2
 */
public class  TireTreeNew {
    public static void main(String[] args) {
        OperateTrieNodes op = new OperateTrieNodes();
        op.insert("abc");
        op.insert("abc");
        op.insert("abc");
        System.out.println(op.insertNum("abc"));
        op.delete("abc");
        System.out.println(op.insertNum("abc"));
        System.out.println(op.prefixNum("ab"));

    }

    public static class OperateTrieNodes{
        public PrefixNode root;
        public OperateTrieNodes(){
            root = new PrefixNode();
        }

        public void insert(String abc) {
            if(abc == null){
                return;
            }
            char[] chars = abc.toCharArray();
            PrefixNode node = root;
            node.pass++;
            for (char aChar : chars) {
              int index=   aChar -'a';
              if(node.next[index] == null){
                  node.next[index] = new PrefixNode();
              }
              node = node.next[index];
              node.pass++;
            }
            node.end++;
        }

        public int insertNum(String abc) {
            if(abc == null){
                return 0;
            }
            char[] chars = abc.toCharArray();
            PrefixNode node = root;
            for (char aChar : chars) {
               int index =  aChar- 'a';
               if(node.next[index] == null){
                   return 0;
               }
               node = node.next[index];

            }
            return node.end;
        }
        public int prefixNum(String abc) {
            if(abc == null){
                return 0;
            }
            char[] chars = abc.toCharArray();
            PrefixNode node = root;
            for (char aChar : chars) {
                int index =  aChar- 'a';
                if(node.next[index] == null){
                    return 0;
                }
                node = node.next[index];
            }
            return node.pass;
        }

        public void delete(String abc) {
            if(abc!=null &&insertNum(abc) != 0 ){
                PrefixNode node  = root;
                node.pass--;
                char[] chars = abc.toCharArray();
                for (char aChar : chars) {
                    int index= aChar-'a';
                    if(--node.next[index].pass == 0){
                        node.next[index] = null;
                    }
                    node = node.next[index];

                }
                node.end--;
            }
        }
    }
    public static class PrefixNode{
        public  PrefixNode[] next;
        public int pass;
        public int end;

        public PrefixNode( ) {
            this.next = new PrefixNode[26];
            this.pass = 0;
            this.end = 0;
        }
    }
}
